package com.goldgov.distancelearning.service.impl;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.goldgov.conference.dao.ConferenceStatDao;
import com.goldgov.conference.service.ConferenceStat;
import com.goldgov.conference.service.ConferenceStatQuery;
import com.goldgov.distancelearning.dao.DistancelearningAttendanceDao;
import com.goldgov.distancelearning.dao.DistancelearningCourseDao;
import com.goldgov.distancelearning.dao.DistancelearningDao;
import com.goldgov.distancelearning.dao.DistancelearningUserDao;
import com.goldgov.distancelearning.service.Distancelearning;
import com.goldgov.distancelearning.service.DistancelearningAttendance;
import com.goldgov.distancelearning.service.DistancelearningBean;
import com.goldgov.distancelearning.service.DistancelearningCountBean;
import com.goldgov.distancelearning.service.DistancelearningCourse;
import com.goldgov.distancelearning.service.DistancelearningCourseQuery;
import com.goldgov.distancelearning.service.DistancelearningQuery;
import com.goldgov.distancelearning.service.DistancelearningService;
import com.goldgov.distancelearning.service.DistancelearningUser;
import com.goldgov.distancelearning.service.DistancelearningUserQuery;
import com.goldgov.meetingstatistics.bean.MeetingParamsBean;
import com.goldgov.synchronous.service.ConferenceSynchronous;
import com.goldgov.utils.DateUtil;
import com.goldgov.utils.PropertyUtil;
@Service
public class DistancelearningServiceImpl implements DistancelearningService {

	@Autowired
	private DistancelearningDao distancelearningDao;
	@Autowired
	private DistancelearningUserDao distancelearningUserDao;
	@Autowired
	private DistancelearningCourseDao distancelearningCourseDao;
	@Autowired
	private DistancelearningAttendanceDao disAttendanceDao;
	@Autowired
	private ConferenceStatDao conferenceStatDao;
	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年6月21日 下午3:55:16 
	 */
	@Override
	public Boolean addDistancelearning(Distancelearning distancelearning) {
		Boolean fase = false;
		int state = distancelearningDao.addDistancelearning(distancelearning);
		if (state >= 1) {
			fase = true;
		}
		return fase;
	}

	/** 
	 * @Description: 新增出席人
	 * @author hanxiangyu
	 * @date 2018年6月21日 下午3:59:02 
	 */
	@Override
	public Boolean addDistancelearningUser(Distancelearning distancelearning) {
		Boolean fase = false;
		List<DistancelearningUser> distancelearningUserList = new ArrayList<>();
		for (String userID : distancelearning.getUserIDs()) {
			DistancelearningUser du = new DistancelearningUser();
			du.setDistancelearningUserID(distancelearningUserDao.preAdd().getDistancelearningUserID());
			du.setUserID(userID);
			du.setDistancelearningID(distancelearning.getDistancelearningID());
			distancelearningUserList.add(du);
		}
		int state = distancelearningUserDao.addDistancelearningUser(distancelearningUserList);
		if (state >= 1) {
			fase = true;
		}
		return fase;
	}

	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年6月21日 下午4:32:19 
	 */
	@Override
	public Distancelearning preAdd() {
		return distancelearningDao.preAdd();
	}

	/** 
	 * @Description: 获取记录
	 * @author hanxiangyu
	 * @date 2018年6月21日 下午5:33:13 
	 */
	@Override
	public List<Distancelearning> getDistancelearningList(DistancelearningQuery query) {
		List<Distancelearning> list = distancelearningDao.getDistancelearningList(query);
		if (!list.isEmpty()) {
			for (Distancelearning distancelearning : list) {
				DistancelearningUser du = new DistancelearningUser();
				du.setDistancelearningID(distancelearning.getDistancelearningID());
				du.setUserID(query.getSearchUserID());
				Distancelearning userStates = distancelearningDao.getUserStates(du);
				if (userStates == null) {
					distancelearning.setUserStatus(3);	//未参加
				}else {
					distancelearning.setUserStatus(userStates.getUserStatus());
				}
			}
		}
		return list;
	}
	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年6月22日 下午5:29:57 
	 */
	@Override
	public List<Distancelearning> getDisStatList(DistancelearningQuery query) {
		return distancelearningDao.getDisStatList(query);
	}
	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年6月21日 下午6:12:24 
	 */
	@Override
	public List<DistancelearningUser> getDistancelearningUserList(DistancelearningUserQuery query) {
		return distancelearningUserDao.getDistancelearningUserList(query);
	}

	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年6月21日 下午6:38:04 
	 */
	@Override
	public List<String> getJoinUserIDs(String distancelearningID) {
		return distancelearningUserDao.getJoinUserIDs(distancelearningID);
	}
	/** 
	 * @Description: 获取会议详情
	 * @author hanxiangyu
	 * @date 2018年6月21日 下午7:11:40 
	 */
	@Override
	public Distancelearning getDistaanceByID(Distancelearning distancelearning) {
		Distancelearning dis = distancelearningDao.getDistaanceByID(distancelearning);
		if (dis != null && dis.getDistancelearningID() != null) {
			DistancelearningUser du = new DistancelearningUser();
			du.setDistancelearningID(distancelearning.getDistancelearningID());
			du.setUserID(distancelearning.getUserID());
			Distancelearning userStates = distancelearningDao.getUserStates(du);
			if (userStates == null) {
				dis.setUserStatus(3);	//未参加
			}else {
				dis.setUserStatus(userStates.getUserStatus());
			}
		}
		return dis;
	}
	@Override
	public Distancelearning getDisByID(String disId) {
		Distancelearning d = new Distancelearning();
		d.setDistancelearningID(disId);
		return distancelearningDao.getDistaanceByID(d);
	}

	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年6月21日 下午7:43:07 
	 */
	@Override
	public List<DistancelearningUser> getExistJoinUserList(Distancelearning distancelearning) {
		return distancelearningUserDao.getExistJoinUserList(distancelearning);
		
	}

	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年6月22日 上午9:32:47 
	 */
	@Override
	public Boolean deleteJoinUser(DistancelearningUser distancelearningUser) {
		Boolean fase = false;
		int state = distancelearningUserDao.deleteJoinUser(distancelearningUser);
		if (state >= 1) {
			fase = true;
		}
		return fase;
	}

	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年6月22日 上午10:23:00 
	 */
	@Override
	public Boolean updateDistancelearning(Distancelearning obj) {
		Boolean fase = false;
		//判断人员信息
		if (obj.getUserIDs() != null && obj.getUserIDs().length > 0) {
			List<DistancelearningUser> exiseUser = distancelearningUserDao.getExistJoinUserList(obj);//查询已存在人员
			if (!exiseUser.isEmpty()) {//判断是否存在已有人员
				List<String> exiseUserID = new ArrayList<>();
				for (DistancelearningUser du : exiseUser) {
					exiseUserID.add(du.getUserID());
				}
				List<String> allUserIDs = new ArrayList<String>(Arrays.asList(obj.getUserIDs()));
				allUserIDs.removeAll(exiseUserID);
				String[] addUserIds = new String[allUserIDs.size()];
				allUserIDs.toArray(addUserIds);
				obj.setUserIDs(addUserIds);
				DistancelearningUser distancelearningUser = new DistancelearningUser();
				distancelearningUser.setDistancelearningID(obj.getDistancelearningID());
				String[] deleteUserIds = new String[exiseUserID.size()];
				exiseUserID.toArray(deleteUserIds);
				distancelearningUser.setUserIDs(deleteUserIds);
				//删除去掉的人员信息
				distancelearningUserDao.deleteJoinUser(distancelearningUser);
				//删除去掉的人员考勤信息
				disAttendanceDao.deleteJoinUser(distancelearningUser);
			}
			if (obj.getUserIDs() != null && obj.getUserIDs().length > 0) {
				this.addDistancelearningUser(obj);
			}
		}
		//判断课程信息
		if (obj.getCourseIDs() != null && obj.getCourseIDs().length > 0) {
			List<DistancelearningCourse> exiseCourse = distancelearningCourseDao.getExiseJoinCourse(obj);
			if (!exiseCourse.isEmpty()) {
				List<String> exiseCourseIds = new ArrayList<>();
				for(DistancelearningCourse dc : exiseCourse) {
					exiseCourseIds.add(dc.getCourseID());
				}
				List<String> allCourseIDs = new ArrayList<String>(Arrays.asList(obj.getCourseIDs()));
				allCourseIDs.removeAll(exiseCourseIds);
				String[] addCourseIds = new String[allCourseIDs.size()];
				obj.setCourseIDs(addCourseIds);
				if (addCourseIds != null && addCourseIds.length > 0) {
					DistancelearningCourse distancelearningCourse = new DistancelearningCourse();
					distancelearningCourse.setCourseIDs(exiseCourseIds); 
					distancelearningCourse.setDistancelearningID(obj.getDistancelearningID());
					distancelearningCourseDao.deleteNoExiseJoinCourse(distancelearningCourse);
				}
			}
			this.addDistancelearningCourse(obj);
		}
		int state = distancelearningDao.updateDistancelearning(obj);
		if (state >= 1) {
			fase = true;
		}
		return fase;
	}

	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年6月22日 上午11:28:44 
	 */
	@Override
	public Boolean publishDistancelearning(Distancelearning distancelearning) {
		Boolean fase = false;
		int state = distancelearningDao.publishDistancelearning(distancelearning);
		if (state >= 1) {
			fase = true;
		}
		return fase;
	}

	/** 
	 * @Description: 获取课件列表
	 * @author hanxiangyu
	 * @date 2018年6月22日 下午3:03:48 
	 */
	@Override
	public List<DistancelearningCourse> getDistancelearningCourseList(DistancelearningCourseQuery query) {
		return distancelearningCourseDao.getDistancelearningCourseList(query);
	}

	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年6月22日 下午3:03:48 
	 */
	@Override
	public Boolean addDistancelearningCourse(Distancelearning obj) {
		Boolean fase = false;
		List<DistancelearningCourse> dcList = new ArrayList<>();
		for (String courseID : obj.getCourseIDs()) {
			DistancelearningCourse dc = new DistancelearningCourse();
			dc.setDistancelearningCourseID(distancelearningCourseDao.preAdd().getDistancelearningCourseID());
			dc.setDistancelearningID(obj.getDistancelearningID());
			dc.setCourseID(courseID);
			dcList.add(dc);
		}
		int state = distancelearningCourseDao.addDistancelearningCourse(dcList);
		if (state >= 1) {
			fase = true;
		}
		return fase;
	}

	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年6月22日 下午4:11:13 
	 */
	@Override
	public List<String> getAllCourseIds(String distancelearningID) {
		return distancelearningCourseDao.getAllCourseIds(distancelearningID);
	}

	/** 
	 * @Description: 回执操作
	 * @author hanxiangyu
	 * @date 2018年6月23日 上午10:02:08 
	 */
	@Override
	public Boolean disReceipt(DistancelearningUser distancelearningUser) {
		Boolean fase = false;
		int state = distancelearningUserDao.disReceipt(distancelearningUser);
		if (state >= 1) {
			fase = true;
		}
		return fase;
	}
	
	
	
	
	
	
	
	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年6月22日 下午4:41:22 
	 */
	
	@Override
	public List<Distancelearning> getDistancelearningByAdminList(DistancelearningQuery query) {
		List<Distancelearning> list = distancelearningDao.getDistancelearningByAdminList(query);
		if (!list.isEmpty()) {
			for (Distancelearning distancelearning : list) {
				DistancelearningUser du = new DistancelearningUser();
				du.setDistancelearningID(distancelearning.getDistancelearningID());
				du.setUserID(query.getSearchUserID());
				Distancelearning userStates = distancelearningDao.getUserStates(du);
				if (userStates == null) {
					distancelearning.setUserStatus(3);	//未参加
				}else {
					distancelearning.setUserStatus(userStates.getUserStatus());
				}
			}
		}
		return list;
		
	}
	
	

	@Override
	public List<Distancelearning> getDisListMobile(DistancelearningQuery query) {
		List<Distancelearning> list = distancelearningDao.getDisListMobile(query);
		if (!list.isEmpty()) {
			for (Distancelearning distancelearning : list) {
				DistancelearningUser du = new DistancelearningUser();
				du.setDistancelearningID(distancelearning.getDistancelearningID());
				du.setUserID(query.getSearchUserID());
				Distancelearning userStates = distancelearningDao.getUserStates(du);
				if (userStates == null) {
					distancelearning.setUserStatus(3);	//未参加
				}else {
					distancelearning.setUserStatus(userStates.getUserStatus());
				}
			}
		}
		return list;
	}
	
	@Override
	public List<Distancelearning> getDisListByOrgId(DistancelearningQuery query) {
		List<Distancelearning> list = distancelearningDao.getDisListByOrgId(query);
		if (!list.isEmpty()) {
			for (Distancelearning distancelearning : list) {
				DistancelearningUser du = new DistancelearningUser();
				du.setDistancelearningID(distancelearning.getDistancelearningID());
				du.setUserID(query.getSearchUserID());
				Distancelearning userStates = distancelearningDao.getUserStates(du);
				if (userStates == null) {
					distancelearning.setUserStatus(3);	//未参加
				}else {
					distancelearning.setUserStatus(userStates.getUserStatus());
				}
			}
		}
		return list;
	}
	

	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年6月26日 上午11:46:48 
	 */
	@Override
	public Boolean addDisAttendance(DistancelearningAttendance distancelearningAttendance) {
		Boolean fase = false;
		int state = disAttendanceDao.addDisAttendance(distancelearningAttendance);
		if (state >= 1) {
			fase = true;
		}
		return fase;
	}

	@Override
	public Boolean deleteDisAttendance(DistancelearningAttendance distancelearningAttendance) {
		Boolean fase = false;
		int state = disAttendanceDao.deleteDisAttendance(distancelearningAttendance);
		if (state >= 1) {
			fase = true;
		}
		return fase;
	}

	@Override
	public Boolean updateDisAttendance(DistancelearningAttendance distancelearningAttendance) {
		Boolean fase = false;
		int state = disAttendanceDao.updateDisAttendance(distancelearningAttendance);
		if (state >= 1) {
			fase = true;
		}
		return fase;
	}

	@Override
	public List<DistancelearningAttendance> getDisAttendance(DistancelearningAttendance distancelearningAttendance) {
		return disAttendanceDao.getDisAttendance(distancelearningAttendance);
	}

	@Override
	public List<DistancelearningUser> getHeadList(String distancelearningID) {
		return distancelearningUserDao.getHeadList(distancelearningID);
	}

	@Override
	public List<DistancelearningUser> getLeaveList(String distancelearningID) {
		return distancelearningUserDao.getLeaveList(distancelearningID);
	}

	@Override
	public List<DistancelearningUser> getSingList(String distancelearningID) {
		return distancelearningUserDao.getSingList(distancelearningID);
	}

	/** 
	 * @Description: 
	 * @date 2018年6月29日 下午3:53:47 
	 */
	@Override
	public List<DistancelearningUser> getDefaultList(String distancelearningID) {
		return distancelearningUserDao.getDefaultList(distancelearningID);
	}

	/** 
	 * @Description: 
	 * @date 2018年6月29日 下午5:11:18 
	 */
	@Override
	public List<Distancelearning> getDisVisStatList(DistancelearningQuery query) {
		return distancelearningDao.getDisVisStatList(query);
	}

	/** 
	 * @Description: 
	 * @author hanxiangyu
	 * @date 2018年7月4日 下午6:19:22 
	 */
	@Override
	public int updateDisById(Distancelearning distancelearning) {
		return distancelearningDao.updateDisById(distancelearning);
	}

	/** 
	 * @Description: 轮播设置查询
	 * @author hanxiangyu
	 * @date 2018年7月4日 下午8:04:39 
	 */
	@Override
	public List<Distancelearning> getDisListByIds(String[] ids) {
		return distancelearningDao.getDisListByIds(ids);
	}

	/** 
	 * @Description: 获取录播列表
	 * @author hanxiangyu
	 * @date 2018年7月5日 下午2:39:12 
	 */
	@Override
	public List<Distancelearning> getLBListByAdmin(DistancelearningQuery query) {
		return distancelearningDao.getLBListByAdmin(query);
	}

	/** 
	 * @Description: 获取录播列表
	 * @author hanxiangyu
	 * @date 2018年7月5日 下午2:39:12 
	 */
	@Override
	public List<Distancelearning> getLBList(DistancelearningQuery query) {
		return distancelearningDao.getLBList(query);
	}

	/** 
	 * @Description: 更新直播信息
	 * @author hanxiangyu
	 * @date 2018年7月6日 下午6:47:04 
	 */
	@Override
	public Boolean updateLiveStatus(Distancelearning distancelearning) {
		Boolean fase = false;
		int state = distancelearningDao.updateLiveStatus(distancelearning);
		if (state >= 1) {
			fase = true;
		}
		return fase;
	}

	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年7月9日 下午3:12:59 
	 */
	@Override
	public Integer getMaxLiveId() {
		Integer maxLiveId = distancelearningDao.getMaxLiveId();
		if (maxLiveId == null ) {
			maxLiveId = new Integer(10000);
		}else {
			maxLiveId = maxLiveId + 1;
		}
		return maxLiveId;
	}

	/** 
	 * @Description: TODO
	 */
	@Override
	public Integer getLiveId() {
		Integer liveId = 0;
		List<Integer> failureLiveId = distancelearningDao.getFailureLiveList();
		List<Integer> validLiveId = distancelearningDao.getValidLiveList();
		if (!failureLiveId.isEmpty()) {
			if (!validLiveId.isEmpty()) {
				for (int j = 0; j < validLiveId.size(); j++) {
					for (int i = 0; i < failureLiveId.size(); i++) {
						if (validLiveId.get(j).intValue() == failureLiveId.get(i).intValue()) {
							failureLiveId.remove(i);
							break;
						}
					}
				}
				if (failureLiveId.isEmpty()) {
					liveId = this.getMaxLiveId();
				}else {
					liveId = Collections.min(failureLiveId);
				}
			}else {
				liveId = Collections.min(failureLiveId);
			}
		}else {
			liveId = this.getMaxLiveId();
		}
		return liveId;
	}

	/** 
	 * @Description: TODO
	 */
	@Override
	public Distancelearning getDisByLiveId(Integer liveId) {
		return distancelearningDao.getDisByLiveId(liveId);
	}

	@Override
	public List<Distancelearning> getDisLiveByAdminList(DistancelearningQuery query) {
		return distancelearningDao.getDisLiveByAdminList(query);
	}

	@Override
	public List<Distancelearning> getRandomDis(DistancelearningBean disBean) {
		return distancelearningDao.getRandomDis(disBean);
	}
	@Override
	public List<Distancelearning> getLBRandomDis(DistancelearningBean disBean) {
		return distancelearningDao.getLBRandomDis(disBean);
	}

	@Override
	public Boolean startAndEndDis(DistancelearningBean disBean) {
		Boolean fase = false;
		int state = distancelearningDao.startAndEndDis(disBean);
		if (state >= 1) {
			fase = true;
		}
		return fase;
	}

	@Override
	public int getLBCountRandomDis(DistancelearningBean disBean) {
		return distancelearningDao.getLBCountRandomDis(disBean);
	}

	@Override
	public int getCountRandomDis(DistancelearningBean disBean) {
		return distancelearningDao.getCountRandomDis(disBean);
	}


	/**
	 * 根据code获取组织结构id
	 * @param scopeCode
	 * @return
	 */
	public String getOrgIdByCode(String scopeCode) {
		return distancelearningDao.getOrgIdByCode(scopeCode);
	}

	@Override
	public List<DistancelearningCountBean> countTimesAndLength(
			DistancelearningQuery query) {
		return distancelearningDao.countTimesAndLength(query);
	}

	@Override
	public List<Distancelearning> findDistancelearningListByOrgId(
			DistancelearningQuery query) {
		return distancelearningDao.findDistancelearningListByOrgId(query);
	}

	@Override
	public List<DistancelearningCountBean> countTimesByTime(
			DistancelearningQuery query) {
		return distancelearningDao.countTimesByTime(query);
	}

	@Override
	public int countOrgNumByOrgId(String orgId) {
		return distancelearningDao.countOrgNumByOrgId(orgId);
	}
	/**
	 * 查询会议下是否存在党员
	 * @date 2018年11月15日 上午10:13:09 
	 * @author hanxiangyu
	 * @param distancelearningUser
	 * @return
	 */
	public List<DistancelearningUser> getJoinUserByUserIds(DistancelearningUser distancelearningUser){
		if (!PropertyUtil.objectNotEmpty(distancelearningUser.getUserIDs())) {
			distancelearningUser.setUserIDs(new String[] {distancelearningUser.getUserID()});
		}
		return distancelearningUserDao.getJoinUserByUserIds(distancelearningUser);
	}
	
	public List<DistancelearningAttendance> getDisAttendanceByUserIdsList(DistancelearningAttendance distancelearningAttendance){
		return disAttendanceDao.getDisAttendanceByUserIdsList(distancelearningAttendance);
	}

	@Override
	public List<Distancelearning> countDistancelearningPercentageRegion(DistancelearningQuery query) throws Exception {
		//设置一个组织在时间段内应完成会议的基数
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
				int quarterValidNum=MeetingParamsBean.REACH_NUM_QUARTER*DateUtil.quarterNum(sdf.parse(query.getSearchBeginDate()), sdf.parse(query.getSearchEndDate()));
				int monthValidNum=MeetingParamsBean.REACH_NUM_MONTH*DateUtil.monthNum(sdf.parse(query.getSearchBeginDate()), sdf.parse(query.getSearchEndDate()));
				query.setSearchQuarterValidNum(quarterValidNum);
				query.setSearchMonthValidNum(monthValidNum);
				//如果是首页的,取的条数小于实际数据
				int pageSize=query.getPageSize();
				query.setPageSize(-1);
				List<Distancelearning> list=distancelearningDao.countDistancelearningPercentageAvg(query);
				//所有会议平均完成度 
				DecimalFormat df = new DecimalFormat("0.00%");
				for(Distancelearning bean:list) {
					ConferenceSynchronous con=bean.getConferenceSynchronous().get(0);
					double distanceLearningCount=0;
					if(PropertyUtil.objectNotEmpty(con.getDistanceLearningCount())&&con.getDistanceLearningCount()>0) distanceLearningCount=con.getDistanceLearningCount();
						double n=(double)distanceLearningCount/(monthValidNum*bean.getOrgNum());
						bean.setAvgNum(n);//借用该字段来排序
						bean.setAvgReachNum(df.format(n));
				}
				//放大不分页,1为按组织排序
				if(Integer.valueOf(1).equals(query.getSearchOrderType())) {
					return list;
				}
				//根据avgReachNum从小到大排序
				List<Distancelearning> list2=list.stream()
						.sorted(Comparator.comparing(Distancelearning::getAvgNum))
						.collect(Collectors.toList());
				if(pageSize>-1&&list2.size()>pageSize) {
					List<Distancelearning> list3=list2.subList(list2.size()-pageSize, list2.size());
					return list3;
				}
				return list2;
	}

	@Override
	public List<Distancelearning> countDistancelearningPercentageAvg(DistancelearningQuery query) throws Exception {
		//设置一个组织在时间段内应完成会议的基数
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		int quarterValidNum=MeetingParamsBean.REACH_NUM_QUARTER*DateUtil.quarterNum(sdf.parse(query.getSearchBeginDate()), sdf.parse(query.getSearchEndDate()));
		int monthValidNum=MeetingParamsBean.REACH_NUM_MONTH*DateUtil.monthNum(sdf.parse(query.getSearchBeginDate()), sdf.parse(query.getSearchEndDate()));
		query.setSearchQuarterValidNum(quarterValidNum);
		query.setSearchMonthValidNum(monthValidNum);
		
		List<Distancelearning> list=distancelearningDao.countDistancelearningPercentageAvg(query);
		//会议类型的平均完成度 
		DecimalFormat df = new DecimalFormat("0.00%");
		for(Distancelearning bean:list) {
			ConferenceSynchronous con=bean.getConferenceSynchronous().get(0);
			if(PropertyUtil.objectNotEmpty(con.getDistanceLearningCount())&&con.getDistanceLearningCount()>0&&bean.getOrgNum()>0) {
				bean.setAvgNum((double)con.getDistanceLearningCount()/(monthValidNum*bean.getOrgNum()));
				bean.setAvgReachNum(df.format((double)con.getDistanceLearningCount()/(monthValidNum*bean.getOrgNum())));
			}else {
				bean.setAvgReachNum("0%");
			}
			
		}
		//修改排序方式,2为按完成度排序
		if(Integer.valueOf(2).equals(query.getSearchOrderType())) {
			List<Distancelearning> list2=new ArrayList<>();
				//根据avgReachNum从大到小排序
				list2=list.stream()
						.sorted(Comparator.comparing(Distancelearning::getAvgNum).reversed())
						.collect(Collectors.toList());
			return list2;
		}
	return list;
	}

	@Override
	public List<Distancelearning> countDistancelearningFinishOrg(DistancelearningQuery query) throws Exception {
		//设置一个组织在时间段内应完成会议的基数
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		int quarterValidNum=MeetingParamsBean.REACH_NUM_QUARTER*DateUtil.quarterNum(sdf.parse(query.getSearchBeginDate()), sdf.parse(query.getSearchEndDate()));
		int monthValidNum=MeetingParamsBean.REACH_NUM_MONTH*DateUtil.monthNum(sdf.parse(query.getSearchBeginDate()), sdf.parse(query.getSearchEndDate()));
		query.setSearchQuarterValidNum(quarterValidNum);
		query.setSearchMonthValidNum(monthValidNum);
		
		query.setPageSize(-1);
		List<Distancelearning> list=distancelearningDao.countDistancelearningPercentageAvg(query);
		List<Distancelearning> resultList=new  ArrayList<Distancelearning>();
		for(Distancelearning bean:list) {
			ConferenceSynchronous con=bean.getConferenceSynchronous().get(0);
			int distanceLearningCount=0;
			if(PropertyUtil.objectNotEmpty(con.getDistanceLearningCount())&&con.getDistanceLearningCount()>0) distanceLearningCount=con.getDistanceLearningCount();
			if(bean.getOrgNum()>0&&distanceLearningCount>=(monthValidNum*bean.getOrgNum())) {
				resultList.add(bean);
			}
		}
		
	return resultList;
	}

	@Override
	public List<Distancelearning> countDistancelearningAttachPercentage(DistancelearningQuery query) {
		query.setPageSize(-1);
		Distancelearning bean=distancelearningDao.countDistancelearningAttachPercentage(query);
		if(!PropertyUtil.objectNotEmpty(bean)) {
			bean=new Distancelearning();
			bean.setAttachOrder("0,0,0,0,0,0,0,0");//没有数据时初始化为0
		}
		String [] attachNums=bean.getAttachOrder().split(",");
		//求和
		int totCount=0;
		for(String num:attachNums) {
			totCount+=Integer.parseInt(num);
		}
		//计算
		DecimalFormat df = new DecimalFormat("0.00%");//格式化小数 
		List<Distancelearning> list=new ArrayList<Distancelearning>();
		for(int i=1;i<=attachNums.length;i++) {
			Distancelearning distancelearning=new Distancelearning();
			distancelearning.setSumNum(totCount);
			distancelearning.setAttachNum(Integer.valueOf(attachNums[i-1]));
			distancelearning.setAttachOrder(String.valueOf(i));
			if(distancelearning.getAttachNum()>0&&distancelearning.getSumNum()>0) {
				distancelearning.setAvgReachNum(df.format((double)distancelearning.getAttachNum()/(double)distancelearning.getSumNum()));
			}else {
				distancelearning.setAvgReachNum("0%");
			}
			list.add(distancelearning);
		}
		return list;
	}
	
	@Override
	public List<ConferenceStat> countDistancelearningPercentageBynum(ConferenceStatQuery query) throws Exception {
		// 查出一共有多少支部
		int orgNum=conferenceStatDao.findOrgNumById(query.getSearchOrganizationID());
		// 查出每个类型的数量分布并算出占比
		DecimalFormat df = new DecimalFormat("0.00%");
		List<ConferenceStat> list=new ArrayList<ConferenceStat>();
			ConferenceStat conferenceStat=conferenceStatDao.countConferencePercentageBynum(query);
			if(!PropertyUtil.objectNotEmpty(conferenceStat)) {
				conferenceStat=new ConferenceStat();
				conferenceStat.setConferenceOrgNum1(0);
				conferenceStat.setConferenceOrgNum2(0);
				conferenceStat.setConferenceOrgNum3(0);
				conferenceStat.setConferenceOrgNum4(0);
			}
			conferenceStat.setOrgNum(orgNum);
			conferenceStat.setConferenceType(String.valueOf(5));
			int num1=conferenceStat.getConferenceOrgNum1();
			int num2=conferenceStat.getConferenceOrgNum2();
			int num3=conferenceStat.getConferenceOrgNum3();
			int num4=conferenceStat.getConferenceOrgNum4();
			String [] orgNumPros=new String[5];
			if((num1+num2+num3+num4)>=orgNum) {
				orgNumPros[0]="0%";
			}else {
				orgNumPros[0]=df.format((double)(orgNum-(num1+num2+num3+num4))/orgNum);
			}
			orgNumPros[1]=df.format((double)num1/orgNum);
			orgNumPros[2]=df.format((double)num2/orgNum);
			orgNumPros[3]=df.format((double)num3/orgNum);
			orgNumPros[4]=df.format((double)num4/orgNum);
			conferenceStat.setConferenceOrgNumPro(orgNumPros);//五种占比
			
			list.add(conferenceStat);
		
		return list;
	}

}
